import Template
import ExcelReader

from Elements import Element
from Enums import Direction
from Enums import ServiceType
from Enums import OpType
from Enums import PayloadType

import CodeWriter

if __name__ == "__main__":
    # test = Element("1",
    #                "2",
    #                Direction.AP_2_CP,
    #                ServiceType.Normal,
    #                OpType.SetRequestNoReturn,
    #                7788,
    #                PayloadType.uint8).
    # 读取xlsx
    workBook = ExcelReader.readBook()
    # 获取单张表格
    sheetNormal = ExcelReader.readNormalSheet(workBook)
    sheetTimeSync = ExcelReader.readTimeSyncSheet(workBook)
    sheetAVAS = ExcelReader.readAVASSheet(workBook)
    sheetHostInfo = ExcelReader.readHostInfoSheet(workBook)
    sheetPower = ExcelReader.readPowerSheet(workBook)
    #sheetMasterPN = ExcelReader.readMasterPNSheet(workBook)
    #sheetSlavePN = ExcelReader.readSlavePNSheet(workBook)
    sheetStruct = ExcelReader.readStructSheet(workBook)
    sheetTextTable = ExcelReader.readTextTableSheet(workBook)
    sheetExtra = ExcelReader.readExtraSheet(workBook)
    # 获取相TX/RX信号
    listNormalTX = ExcelReader.readSheetTX(sheetNormal)
    listNormalRX = ExcelReader.readSheetRX(sheetNormal)
    listTimeSyncTX = ExcelReader.readSheetTX(sheetTimeSync)
    listTimeSyncRX = ExcelReader.readSheetRX(sheetTimeSync)
    listAVASTX = ExcelReader.readSheetTX(sheetAVAS)
    listAVASRX = ExcelReader.readSheetRX(sheetAVAS)
    listHostInfoTX = ExcelReader.readSheetTX(sheetHostInfo)
    listHostInfoRX = ExcelReader.readSheetRX(sheetHostInfo)
    listPowerTX = ExcelReader.readSheetTX(sheetPower)
    listPowerRX = ExcelReader.readSheetRX(sheetPower)
    #listMasterPNTX = ExcelReader.readSheetTX(sheetMasterPN)
    #listMasterPNRX = ExcelReader.readSheetRX(sheetMasterPN)
    #listSlavePNTX = ExcelReader.readSheetTX(sheetSlavePN)
    #listSlavePNRX = ExcelReader.readSheetRX(sheetSlavePN)
    # 结构体全部
    listStruct = ExcelReader.readStructContent(sheetStruct)
    # 枚举全部
    listTexttable = ExcelReader.readTexttableContent(sheetTextTable)
    # 额外标注
    listExtraPull = ExcelReader.readExtraPull(sheetExtra)
    listExtraSDB = ExcelReader.readExtraSDB(sheetExtra)

    listTXSignalIds: Element = []
    listRXSignalIds: Element = []
    CodeWriter.addSignalIds(listTXSignalIds, listNormalTX, listStruct, listExtraPull)
    CodeWriter.addSignalIds(listTXSignalIds, listTimeSyncTX, listStruct, listExtraPull)
    CodeWriter.addSignalIds(listTXSignalIds, listAVASTX, listStruct, listExtraPull)
    CodeWriter.addSignalIds(listTXSignalIds, listHostInfoTX, listStruct, listExtraPull)
    CodeWriter.addSignalIds(listTXSignalIds, listPowerTX, listStruct, listExtraPull)
    #CodeWriter.addSignalIds(listTXSignalIds, listMasterPNTX, listStruct, listExtraPull)

    CodeWriter.addSignalIds(listRXSignalIds, listNormalRX, listStruct, listExtraPull)
    CodeWriter.addSignalIds(listRXSignalIds, listTimeSyncRX, listStruct, listExtraPull)
    CodeWriter.addSignalIds(listRXSignalIds, listAVASRX, listStruct, listExtraPull)
    CodeWriter.addSignalIds(listRXSignalIds, listHostInfoRX, listStruct, listExtraPull)
    CodeWriter.addSignalIds(listRXSignalIds, listPowerRX, listStruct, listExtraPull)
    #CodeWriter.addSignalIds(listRXSignalIds, listMasterPNRX, listStruct, listExtraPull)
    # ------------------------------------------------------------
    # 覆盖相关代码文件
    file_APCPSignal_Id_h = CodeWriter.readApCpSignalId()
    file_Normal_h = CodeWriter.readNormalHeader()
    file_TimeSync_h = CodeWriter.readTimeSyncHeader()
    file_AVAS_h = CodeWriter.readAVASHeader()
    file_HostInfo_h = CodeWriter.readHostInfoHeader()
    file_Power_h = CodeWriter.readPowerHeader()
    #file_MasterPN_h = CodeWriter.readMasterPNHeader()
    #file_SlavePN_h = CodeWriter.readSlavePNHeader()
    file_APCP_Transfer_h = CodeWriter.readAPCPTransferHeader()
    file_APCP_Signal_h = CodeWriter.readAPCPSignalHeader()
    file_APCP_Signal_cpp = CodeWriter.readAPCPSignalCpp()
    file_convert_TX_Signal_h = CodeWriter.readConvert2IPCP()
    file_convert_RX_IPCP_h = CodeWriter.readConvertFromIPCP()
    # ==============================================================
    # 开始写入各个文件
    # ==============================================================
    # 写入Signal_id.h文件
    CodeWriter.writeSignalIdHeader(file_APCPSignal_Id_h)
    CodeWriter.writeSignalIdTX(file_APCPSignal_Id_h, listTXSignalIds)
    CodeWriter.writeSignalIdTxRxDivider(file_APCPSignal_Id_h)
    CodeWriter.writeSignalIdRX(file_APCPSignal_Id_h, listRXSignalIds)
    CodeWriter.writeSignalIDEnumsDivider(file_APCPSignal_Id_h)
    CodeWriter.writeSignalIdEnums(file_APCPSignal_Id_h, listTexttable, listExtraSDB)
    CodeWriter.writeSignalIdFoot(file_APCPSignal_Id_h)
    file_APCPSignal_Id_h.close()
    print("[APCP]写入APCP_Signal_Id.h成功")
    # 写入Normal.h文件
    CodeWriter.writeHeaderPragma(file_Normal_h)
    CodeWriter.writeHeaderOpCode(file_Normal_h, listNormalTX, listNormalRX)
    CodeWriter.writeHeaderStruct(file_Normal_h, listNormalTX, listNormalRX, listStruct)
    file_Normal_h.close()
    print("[APCP]写入Normal.h成功")
    # 写入TimeSync.h文件
    CodeWriter.writeHeaderPragma(file_TimeSync_h)
    CodeWriter.writeHeaderOpCode(file_TimeSync_h, listTimeSyncTX, listTimeSyncRX)
    CodeWriter.writeHeaderStruct(file_TimeSync_h, listTimeSyncTX, listTimeSyncRX, listStruct)
    file_TimeSync_h.close()
    print("[APCP]写入TimeSync.h成功")
    # 写入AVAS.h文件
    CodeWriter.writeHeaderPragma(file_AVAS_h)
    CodeWriter.writeHeaderOpCode(file_AVAS_h, listAVASTX, listAVASRX)
    CodeWriter.writeHeaderStruct(file_AVAS_h, listAVASTX, listAVASRX, listStruct)
    file_AVAS_h.close()
    print("[APCP]写入AVAS.h成功")

    # 写入HostInfo.h文件
    CodeWriter.writeHeaderPragma(file_HostInfo_h)
    CodeWriter.writeHeaderOpCode(file_HostInfo_h, listHostInfoTX, listHostInfoRX)
    CodeWriter.writeHeaderStruct(file_HostInfo_h, listHostInfoTX, listHostInfoRX, listStruct)
    file_HostInfo_h.close()
    print("[APCP]写入HostInfo.h成功")

    # 写入Power.h文件
    CodeWriter.writeHeaderPragma(file_Power_h)
    CodeWriter.writeHeaderOpCode(file_Power_h, listPowerTX, listPowerRX)
    CodeWriter.writeHeaderStruct(file_Power_h, listPowerTX, listPowerRX, listStruct)
    file_HostInfo_h.close()
    print("[APCP]Power.h成功")

    # 写入NetAPCP.h
    CodeWriter.writeHeaderOpIds(file_APCP_Transfer_h,
                                listNormalTX, listNormalRX,
                                listTimeSyncTX, listTimeSyncRX,
                                listAVASTX, listAVASRX,
                                listHostInfoTX, listHostInfoRX,
                                listPowerTX, listPowerRX)
    print("[APCP]写入NetAPCP.h成功")

    CodeWriter.write_Signal_h_Front(file_APCP_Signal_h)
    CodeWriter.write_Signal_h_Typedef(file_APCP_Signal_h, listNormalTX, listNormalRX)
    CodeWriter.write_Signal_h_Typedef(file_APCP_Signal_h, listTimeSyncTX, listTimeSyncRX)
    CodeWriter.write_Signal_h_Typedef(file_APCP_Signal_h, listAVASTX, listAVASRX)
    CodeWriter.write_Signal_h_Typedef(file_APCP_Signal_h, listHostInfoTX, listHostInfoRX)
    CodeWriter.write_Signal_h_Typedef(file_APCP_Signal_h, listPowerTX, listPowerRX)
    CodeWriter.write_Signal_h_Body(file_APCP_Signal_h)
    CodeWriter.write_Signal_h_Function(file_APCP_Signal_h, listTXSignalIds, listRXSignalIds, listExtraSDB)
    CodeWriter.write_Signal_h_End(file_APCP_Signal_h)
    file_APCP_Signal_h.close()
    print("[APCP]写入APCPSignalManager.h成功")

    CodeWriter.write_Signal_cpp_Front(file_APCP_Signal_cpp)
    CodeWriter.write_Signal_cpp_Function(file_APCP_Signal_cpp, listTXSignalIds, listRXSignalIds, listExtraSDB)
    CodeWriter.write_Signal_cpp_End(file_APCP_Signal_cpp)
    file_APCP_Signal_cpp.close()
    print("[APCP]写入APCPSignalManager.cpp成功")

    CodeWriter.write_2_ipcp_front(file_convert_TX_Signal_h)
    CodeWriter.write_2_ipcp_case(file_convert_TX_Signal_h, listTXSignalIds)
    CodeWriter.write_2_ipcp_end(file_convert_TX_Signal_h)
    file_convert_TX_Signal_h.close()
    print("[APCP]apcp_signal_2_ipcp_.h成功")

    CodeWriter.write_2_signal_front(file_convert_RX_IPCP_h)
    CodeWriter.write_convert_RX_signal_case(file_convert_RX_IPCP_h, listNormalRX, listStruct, listExtraPull)
    CodeWriter.write_convert_RX_signal_case(file_convert_RX_IPCP_h, listTimeSyncRX, listStruct, listExtraPull)
    CodeWriter.write_convert_RX_signal_case(file_convert_RX_IPCP_h, listAVASRX, listStruct, listExtraPull)
    CodeWriter.write_convert_RX_signal_case(file_convert_RX_IPCP_h, listHostInfoRX, listStruct, listExtraPull)
    CodeWriter.write_convert_RX_signal_case(file_convert_RX_IPCP_h, listPowerRX, listStruct, listExtraPull)
    CodeWriter.write_2_signal_end(file_convert_RX_IPCP_h)
    file_convert_RX_IPCP_h.close()
    print("[APCP]apcp_ipcp_2_signal.h成功")
